package com.service;


import com.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.*;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Service;

import java.util.List;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class StudentService {

    @Autowired
    private SolrTemplate solrTemplate;

    /**
     * 新增和修改
     *
     * @param stu
     */
    public void add(Student stu) {
        //
        solrTemplate.saveBean("core2", stu);
        solrTemplate.commit("core2");
    }


    public void del(int id) {
        solrTemplate.deleteByIds("core2", id + "");
        solrTemplate.commit("core2");
    }

    public List search(String des, int page, int size) {

        SimpleHighlightQuery query = new SimpleHighlightQuery(new SimpleStringCriteria("des:" + des));
        // 配置高亮选项
        HighlightOptions options = new HighlightOptions();
        // 高亮域
        options.addField("des");
        // 前缀和后缀
        options.setSimplePrefix("<span style=\"color:red;\">");
        options.setSimplePostfix("</span>");

         // 设置高亮设置
        query.setHighlightOptions(options);
        HighlightPage<Student> students = solrTemplate.queryForHighlightPage("core2", query, Student.class);
        //---------------------以上高亮显示---------------------------

        //Query query = new SimpleQuery("des:"+des);
        //ScoredPage<Student> students = solrTemplate.queryForPage("core2",query,Student.class);


        int total = students.getSize();

        List<Student> list = students.toList();
        int start = (page - 1) * size;
        int end = start + size > total ? total : start + size;
        return list.subList(start, end);
    }
}





